package org.jzs.mybaseapp.section.home.costomview;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

import org.jzs.mybaseapp.R;
import org.jzs.mybaseapp.common.widget.expandtab.ViewBaseAction;
import org.jzs.mybaseapp.section.home.entity.ExpandTabEntity.DataEntity.CatEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 类型
 *
 * @author Zhanqx
 */
public class LxView extends RelativeLayout implements ViewBaseAction {

    private ListView mListView;
    private String[] items;// 显示字段
    private String[] itemsVaule;// 隐藏id
    private OnSelectListener mOnSelectListener;
    private TextAdapter adapter;
    private String mDistance;
    private String showText = "item1";
    private Context mContext;
    private List<CatEntity> list;
    private String cat_id;

    public String getShowText() {
        return showText;
    }

    public LxView(Context context, List<CatEntity> list, String cat_id) {
        super(context);
        if (!TextUtils.isEmpty(cat_id)) {
            this.cat_id = cat_id;
        }
        this.list = list;
        init(context);
    }

    public LxView(Context context, AttributeSet attrs, int defStyle, List<CatEntity> list, String cat_id) {
        super(context, attrs, defStyle);
        if (!TextUtils.isEmpty(cat_id)) {
            this.cat_id = cat_id;
        }
        this.list = list;
        init(context);
    }

    public LxView(Context context, AttributeSet attrs, List<CatEntity> list, String cat_id) {
        super(context, attrs);
        if (!TextUtils.isEmpty(cat_id)) {
            this.cat_id = cat_id;
        }
        this.list = list;
        init(context);
    }

    private int checkPos = 0;

    private void init(Context context) {
        if (list == null) {
            list = new ArrayList<CatEntity>();
        }
        CatEntity datas = new CatEntity();
//		datas.setIcon("");
        datas.catId = "";
        datas.catName = "全部类型";
        list.add(0, datas);

        items = new String[list.size()];
        itemsVaule = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            items[i] = list.get(i).catName;
            itemsVaule[i] = list.get(i).catId + "";
            if (list.get(i).catId.equals(cat_id)) {
                checkPos = i;
            }
        }

        mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View inflate = inflater.inflate(R.layout.view_distance, this, true);
        inflate.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        // setBackgroundDrawable(getResources().getDrawable(R.drawable.choosearea_bg_mid));
        mListView = (ListView) findViewById(R.id.listView);
        adapter = new TextAdapter(context, items, R.drawable.choose_item_right, R.drawable.choose_eara_item_selector);
        adapter.setTextSize(14);
        adapter.setSelectedPositionNoNotify(checkPos);
        if (mDistance != null) {
            for (int i = 0; i < itemsVaule.length; i++) {
                if (itemsVaule[i].equals(mDistance)) {
                    adapter.setSelectedPositionNoNotify(i);
                    showText = items[i];
                    break;
                }
            }
        }
        mListView.setAdapter(adapter);
        adapter.setOnItemClickListener(new TextAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {

                if (mOnSelectListener != null) {
                    showText = items[position];
                    mOnSelectListener.getValue(itemsVaule[position], items[position], position);
//					LogUtils.e("类型:::::" + itemsVaule[position] + "" +  items[position]+ ""+ position);
                }
            }
        });
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        mOnSelectListener = onSelectListener;
    }

    public interface OnSelectListener {
        public void getValue(String checkId, String value, int position);
    }

    @Override
    public void hide() {

    }

    @Override
    public void show() {

    }

}
